<?php

/**
* -------------------------------------------------------------------------------------------------------------------
* @abstract   Handles the CRUD operations for Service model and other methods
* @package    Kiropedic
* @subpackage Controllers
* @author     Darwing Hernández - darwing.her69@gmail.com
* -------------------------------------------------------------------------------------------------------------------
*/

class ServiceController extends BaseController
{

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * ATTRIBUTES AND CONSTANTS
  * ---------------------------------------------------------------------------------------------------------------
  */
  public static $restful = true;

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * VIEWS
  * ---------------------------------------------------------------------------------------------------------------
  */

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * CRUD OPERATIONS
  * ---------------------------------------------------------------------------------------------------------------
  */

  /**
  * CREATE OPERATION
  */
  public function register()
  {
    $input  = Input::all();
    $index  = array('codctrl');
    $values = array('unique:service_srv,srv_codctrl');

    if (!$this->validate($input, CREATE_SERV, $index, $values))
      return  Redirect::to('admin/create/serv/')
                      ->with('notification', 'register_add_warning')
                      ->with('opt', 'El servicio ')
                      ->withErrors($this->validation)
                      ->withInput();

    $service = Service::create(array(
      'srv_name'        =>  Input::get('nombre'),
      'srv_codctrl'     =>  Input::get('codctrl'),
      'srv_description' =>  Input::get('descrip'),
      'srv_priceunit'   =>  Input::get('precio'),
      'srv_active'      =>  Input::get('active') == "on"? 1: 0
    ));

    if(Input::get('again') == "on")
      return Redirect::to('admin/create/serv')->with('notification', 'register_add_success')->with('opt', 'El servicio ');
    else
      return Redirect::to('admin/list/listserv')->with('notification', 'register_add_success')->with('opt', 'El servicio ');
  }

  public function view($id)
  {
    $service = Service::select('srv_code', 'srv_name', 'srv_codctrl', 'srv_description', 'srv_priceunit', 'srv_active')->find($id);

    if(!$service) return Redirect::to('admin/list/listserv')->with('notification', 'not_allowed');

    return View::make('admin.service.editserv')->with('service', $service)->with('title', 'Editar Servicio')->with('active', 4);
  }

  public function update($id)
  {
    $input = Input::all();
    $index  = array('codctrl');
    $values = array('unique:service_srv,srv_codctrl,'.$id.',srv_code');

    if (!$this->validate($input, UPDATE_SERV, $index, $values))
      return Redirect::to('admin/viewServ/'.$id)->withErrors($this->validation)->withInput()->with('notification', 'register_edit_warning')->with('opt', 'El servicio ');

    $service                  = Service::find($id);
    $service->srv_name        = Input::get('nombre');
    $service->srv_codctrl     = Input::get('codctrl');
    $service->srv_description = Input::get('descrip');
    $service->srv_priceunit   = Input::get('precio');
    $service->srv_active      = Input::get('active') == "on"? 1: 0;
    $service->save();

    return Redirect::to('admin/viewServ/'.$id)->with('notification', 'register_edit_success')->with('opt', 'El servicio ');
  }

  public static function delete($keys)
  {
    Service::destroy($keys);
    return Redirect::to('admin/list/listserv')->with('notification', 'register_delete_success');
  }

  /**
  * ---------------------------------------------------------------------------------------------------------------
  * OTHER METHODS/ACTIONS AND STUFF
  * ---------------------------------------------------------------------------------------------------------------
  */

	public function displayServices()
	{
    return View::make('kir.welcome')->with('services', Service::all())->with('title', 'Selección de Servicios');
	}

	public function selectServices()
	{
		$keys = preg_split("/[\s@]+/", Input::get('d077f244def8a70'));
    $codes = array(count($keys)/2);

    for($i = 0; $i < count($keys); $i=$i+2)
        $codes[$i/2] = $keys[$i];

    $services  = Service::select('srv_name', 'srv_type')->whereIn('srv_code', $codes);
    $operators = Operator::select('ope_code', 'ope_name', 'ope_image')->get();

    return View::make('kir.chooseOperator')->with('operators', $operators)->with('title', 'Selección de Operador');
	}
}